﻿using System;

namespace TiaoTiaoCode.Common.Extensions
{
    /// <summary>
    /// 时间类型扩展
    /// </summary>
    public static class DateTimeEx
    {
        public const string DateLink = "-";

        public const string DateLinkEx = "/";

        public const string TimeLink = ":";

        public const string DateTimeFormatEx = "yyyy-MM-dd HH:mm:ss.fff";

        public const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

        public const string DateFormat = "yyyy-MM-dd";

        public const string TimeFormat = "HH:mm:ss";

        /// <summary>
        /// 计算版本号的原点时间
        /// </summary>
        readonly static DateTime _versionOriginTime = new DateTime(2000, 1, 1, 0, 0, 0);

        /// <summary>
        /// 转当天零点
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime TiaoTiaoToMorning(this DateTime dt)
        {
            return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0);
        }

        /// <summary>
        /// 转当天最后一秒
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime TiaoTiaoToNight(this DateTime dt)
        {
            return new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59);
        }

        /// <summary>
        /// 转当天零点
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime? TiaoTiaoToMorning(this DateTime? dt)
        {
            if (dt == null) return null;
            var tmp = (DateTime)dt;
            return new DateTime(tmp.Year, tmp.Month, tmp.Day, 0, 0, 0);
        }

        /// <summary>
        /// 转当天最后一秒
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime? TiaoTiaoToNight(this DateTime? dt)
        {
            if (dt == null) return null;
            var tmp = (DateTime)dt;
            return new DateTime(tmp.Year, tmp.Month, tmp.Day, 23, 59, 59);
        }

        /// <summary>
        /// 转成常用时间格式串
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string TiaoTiaoToNormal(this DateTime dt)
        {
            return dt.ToString(DateTimeFormat);
        }

        /// <summary>
        /// 转成常用时间格式串
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string TiaoTiaoToNormal(this DateTime? dt)
        {
            if (dt == null) return "1970-01-01 00:00:00";

            return ((DateTime)dt).ToString(DateTimeFormat);
        }

        #region .Net版本号相关

        /// <summary>
        /// 时间转修订版本号
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static int TiaoTiaoMinorNumber(this DateTime dt)
        {
            return (int)(dt - _versionOriginTime).TotalDays;
        }

        /// <summary>
        /// 时间转编译版本号
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static int TiaoTiaoBuilerNumber(this DateTime dt)
        {
            return (int)(dt - dt.TiaoTiaoToMorning()).TotalSeconds / 2;
        }

        #endregion
    }
}
